﻿using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.ServiceContracts;
using KendoGridBinder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkTicket.Application.Implementation;
using WorkTicket.Website.Common;

namespace WorkTicket.Website.Controllers
{
    public class SizeController : Controller
    {
        protected ISizeService sizeService = new SizeServiceImpl();
        //
        // GET: /Size/

        public ActionResult Index()
        {
            return View();
        }


        public JsonResult GetAllItems(KendoGridRequest request)
        {
            List<Size> result = sizeService.GetAll();
            return Json(new { data = result, total = result.Count },JsonRequestBehavior.AllowGet); 
        }

        //
        // GET: /Size/Create
        [HttpGet]
        public ActionResult Create()
        {
            Size size = new Size();
            return View(size);
        }

        //
        // POST: /Size/Create

        [HttpPost]
        public ActionResult CreateAndEdit(Size model)
        {
            try
            {
                // TODO: Add insert logic here
                if (Request.IsAjaxRequest())
                {
                    if (model.SizeID.ToString() == "00000000-0000-0000-0000-000000000000") //add new entity
                    {
                        model.SizeID = Guid.NewGuid();
                        if (ModelState.IsValid)
                        {
                            sizeService.Add(model);
                        }
                    }
                    else  //update entity
                    {
                        if (ModelState.IsValid)
                        {
                            sizeService.Update(model);
                        }
                    }
                    
                }
                return Json(new { data = true, message = "OK" }, JsonRequestBehavior.AllowGet); 
            }
            catch
            {
                return Json(new { data = false, message = "Failed" }, JsonRequestBehavior.AllowGet); 
            }
        }

        //
        // GET: /Size/Edit/5
        [HttpGet]
        public ActionResult Edit(string id)
        {
            var size= sizeService.GetItemByID(id);
            return View("Create",size);
        }

        //
        // GET: /Size/Delete/5
        public ActionResult Delete(string id)
        {
            try
            {
                sizeService.Delete(id);
                return Json(new { message = "ok" },JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { message = "no" }, JsonRequestBehavior.AllowGet);
            }
        }


        [HttpPost]
        public JsonResult CheckNameExisted()
        {
            string name = Request["Name"];
            string id = Request["ID"];
            JsonResult result = new JsonResult();
            result.Data = sizeService.CheckNameExisted(name, Guid.Parse(id));
            return result;
        }

    }
}
